﻿using B.S.BasicData.Infrastcuture.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.S.BasicData.Infrastcuture.Implements
{

    /// <summary>
    /// 基础基类
    /// </summary>
    /// <typeparam name="T">实体类</typeparam>
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {

        /// <summary>
        /// 注册上下文
        /// </summary>
        private readonly MyDbContext context;

        /// <summary>
        /// /构造函数
        /// </summary>
        /// <param name="context">上下文</param>
        public BaseRepository(MyDbContext context)
        {
            this.context = context;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回任务</returns>
        public async Task<int> Add(T entity)
        {
            context.Set<T>().Add(entity);
           return await context.SaveChangesAsync();
        }
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns>返回任务</returns>
        public IQueryable<T> GetAll()
        {
            return context.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回任务</returns>
        public Task<int> Update(T entity)
        {
            context.Set<T>().Update(entity);
            return context.SaveChangesAsync();
        }
    }
}
